/**
 * @Author：ming
 * @Date：2023/9/29 4:16 PM
 * @Filename：Lc200
 */

/**
 * @author ming
 */
public class Lc200 {
    public int numIslands(char[][] grid) {
        int len = grid.length;
        int rlen = grid[0].length;
        int count =0;
        for (int i = 0; i < len; i++) {
            for (int j = 0; j < rlen; j++) {
                if(getArea(grid,i,j)>=1){
                    count++;
                }
            }
        }
        return count;
    }

    public int getArea(char[][] a,int r,int c){
        if(!inFind(a,r,c)){
            return 0;
        }

        if(a[r][c]=='0'){
            return 0;
        }

        if(a[r][c]!='1'){
            return 0;
        }

        a[r][c]='2';

        return 1
                +getArea(a,r,c-1)
                +getArea(a,r,c+1)
                +getArea(a,r-1,c)
                +getArea(a,r+1,c);
    }

    public boolean inFind(char[][] a ,int r,int c){
        return r>=0&&r<a.length
                && c<=a[0].length&&c>=0;
    }
}
